package com.sdcqjy.property.home.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sdcqjy.mylibrary.OtherUtils;
import com.sdcqjy.mylibrary.widget.SpacesItemDecoration;
import com.sdcqjy.property.R;
import com.sdcqjy.property.activity.DetailsActivity;
import com.sdcqjy.property.activity.MessageDetailsActivity;
import com.sdcqjy.property.adapter.MessageAdapter;
import com.sdcqjy.property.base.BaseFragment;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.mode.MessageMode;
import com.sdcqjy.property.presenter.MessagePresenter;
import com.sdcqjy.property.presenter.contract.MessageContract;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 磁磁帅 on 2018/5/13.
 * 功能：
 */

public class MessageFragment extends BaseFragment<MessagePresenter> implements MessageContract.View, MessageAdapter.OnAdapterClickListener {
    @BindView(R.id.listView)
    RecyclerView listView;
    Unbinder unbinder;
    private int type;
    private List<MessageMode> modeList;
    private MessageAdapter adapter;

    //isRead 0代表未读  1代表已读
    public static MessageFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        MessageFragment fragment = new MessageFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    public MessageFragment() {
        presenter = new MessagePresenter(this);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle bundle = getArguments();
        type = bundle.getInt("type", 0);

        modeList = new ArrayList<>();
        adapter = new MessageAdapter(getActivity(), modeList).setOnAdapterClickListener(this);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        listView.addItemDecoration(new SpacesItemDecoration(OtherUtils.dip2px(getActivity(), 1), new boolean[]{false, false, false, true}));

        presenter.getList(type);
    }

    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void getListRet(List<MessageMode> data) {
        if (data == null) data = new ArrayList<>();
        modeList.clear();
        modeList.addAll(data);
        adapter.notifyDataSetChanged();
    }

    public void onAdapterClicked(MessageMode mode) {
        presenter.UpdateMessage(mode.id);
        if (mode.documentID == 0) {
            MessageDetailsActivity.open(getActivity(), mode);
        } else {
            presenter.getDetailsMode(mode.documentID + "");
        }
    }

    public void getDetailsModeRet(ListMsgMode data) {
        DetailsActivity.open(getActivity(), data);
    }
}
